package com.tsing.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class TcpUdpUtil {

    public static void sendStrByTCP(String ip, Integer port, String content) throws Exception {
        Socket sc = new Socket(ip, port);//创建socket连接
        OutputStream os = sc.getOutputStream();//发送消息IO流
        byte[] bytes = content.getBytes("GBK");  //转化为字节流
        os.write(bytes);
        os.flush();
        os.close();
        sc.close();
    }

    public static void sendHexStrByTCP(String ip, Integer port, String content) throws Exception {
        Socket sc = new Socket(ip, port);//创建socket连接
        OutputStream os = sc.getOutputStream();//发送消息IO流
        byte[] bytes = hexStrToByteStr(content);
        os.write(bytes);
        os.flush();
        os.close();
        sc.close();
    }

    public static String sendHexStrByTCPAndReceive(String ip, Integer port, String content) throws Exception {
        String result = "";
        Socket sc = new Socket(ip, port);//创建socket连接
        OutputStream os = sc.getOutputStream();//发送消息IO流
        InputStream is = sc.getInputStream();//接收消息IO流
        byte[] bytes = hexStrToByteStr(content);
        os.write(bytes);
        os.flush();
        sc.shutdownOutput();
        //接收服务器的响应
        int line = 0;
        byte[] buff = new byte[is.available()];
        //接收收到的数据
        while ((line = is.read(buff)) != -1) {
            //将字节数组转换成十六进制的字符串
            result = BinaryToHexString(buff);
        }
        is.close();
        os.close();
        sc.close();
        return result;
    }

    public static void sendStrByUDP(String ip, Integer port, String content) throws Exception {
        //创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        byte[] bytes = content.getBytes("GBK");  //转化为字节流
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), port);
        ds.send(dp);
        ds.close();
    }

    public static void sendHexStrByUDP(String ip, Integer port, String content) throws Exception {
        //创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        byte[] bytes = hexStrToByteStr(content);
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), port);
        ds.send(dp);
        ds.close();
    }

    public static String sendHexStrByUDPAndReceive(String ip, Integer port, String content) throws Exception {
        String result = "";
        //创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        byte[] bytes = hexStrToByteStr(content);
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), port);
        ds.send(dp);
        ds.close();
        DatagramSocket ds2 = new DatagramSocket(port);
        //接收服务器的响应
        while (StringUtils.isBlank(result)) {
            byte[] buff = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(buff, buff.length);
            ds2.receive(dp2);
            result = BinaryToHexString(buff);
            System.out.println(result);
        }
        ds2.close();
        return result;
    }

    /**
     * 将十六进制的字符串转换成字节数组
     */
    public static byte[] hexStrToByteStr(String hexString) {
        if (TextUtils.isBlank(hexString)) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        int index = 0;
        byte[] bytes = new byte[len / 2];
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
            index += 2;
        }
        return bytes;
    }

    /**
     * 将字节数组转换成十六进制的字符串
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }

}
